package com.credithc.signature.manager;

import com.credithc.signature.common.utils.UUIDUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2020年05⽉15⽇<br/>
 * @version：v1.0.0<br>
 */
public class BuildRequestParams {

    public static void main(String[] args) {
        String requestUrl = "http://test.contract.pinkecard.com/";
        String requestUrl2 = "http://signature.web.hcinner";
        String systemSign = "jflow";
        String requestNo = UUIDUtil.getUUID();
        String fileUrl = "b38d2bfacce6439caf89b56477fa5112";
        String sealId = "OFFICE_58565598";
        String timestamp = System.currentTimeMillis() + "";
        String callbackUrl = "http://10.150.27.191:8080/jflow-web/WF/Comm5/callback.do";
        String fileSys = "s3";
        String SEQUENCE_KEY = "b9cf62d17f1c4523ad4945a4e03c273a";

        StringBuilder builder = new StringBuilder();
        String str = builder.append(systemSign).append(requestNo).append(fileUrl).append(sealId).append(timestamp).append(callbackUrl).
                append(fileSys).append(SEQUENCE_KEY).toString();

        String value = DigestUtils.sha1Hex(str);
        String url = requestUrl + "?systemSign=" + systemSign + "&requestNo=" + requestNo + "&" +
                "signature=" + value + "&timestamp=" + timestamp + "&sealId=" + sealId + "&fileUrl=" +
                fileUrl + "&callbackUrl=" + callbackUrl + "&fileSys=" + fileSys;

        System.out.println(url);
    }

    @Test
    public void test1(){
        String params = "http://signature.web.hcinner/?systemSign=jflow&requestNo=d72f46d82423423f804356c3c3100fd8&signature=fbfcba1c197933bccabbe761f4483d58c59bea61&timestamp=1591346263935&sealId=OFFICE_31936747&fileUrl=d72f46d82423423f804356c3c3100fd8&callbackUrl=http%3A%2F%2F10.10.255.87%3A8080%2Fjflow-web%2FWF%2FComm5%2Fcallback.do&fileSys=S3";
        String[] split = params.split("&");

        String requestUrl = "";
        String systemSign = "";
        String requestNo = "";
        String fileUrl = "";
        String sealId = "";
        String timestamp = "";
        String callbackUrl = "http://www.baidu.com";
        String fileSys = "";
        String SEQUENCE_KEY = "b9cf62d17f1c4523ad4945a4e03c273a";
        for (String s : split) {
            if(s.contains("http://")){
                String[] split1 = s.split("\\?");
                requestUrl = split1[0];
                String[] split2 = split1[1].split("=");
                systemSign = split2[1];
                continue;
            }
            if(s.contains("requestNo")){
                String[] split2 = s.split("=");
                requestNo = split2[1];
                continue;
            }
            if(s.contains("timestamp")){
                String[] split2 = s.split("=");
                timestamp = System.currentTimeMillis() + "";
                continue;
            }
            if(s.contains("sealId")){
                String[] split2 = s.split("=");
                sealId = split2[1];
            }
            if(s.contains("fileUrl")){
                String[] split2 = s.split("=");
                fileUrl = split2[1];
                continue;
            }
            if(s.contains("callbackUrl")){
                String[] split2 = s.split("=");
//                callbackUrl = split2[1];
                callbackUrl = "http://www.baidu.com";
                continue;
            }
            if(s.contains("fileSys")){
                String[] split2 = s.split("=");
                fileSys = split2[1];
                continue;
            }
        }

        StringBuilder builder = new StringBuilder();
        String str = builder.append(systemSign).append(requestNo).append(fileUrl).append(sealId).append(timestamp).append(callbackUrl).
                append(fileSys).append(SEQUENCE_KEY).toString();

        String value = DigestUtils.sha1Hex(str);

        String url = requestUrl + "?systemSign=" + systemSign + "&requestNo=" + requestNo + "&" +
                "signature=" + value + "&timestamp=" + timestamp + "&sealId=" + sealId + "&fileUrl=" +
                fileUrl + "&callbackUrl=" + callbackUrl + "&fileSys=" + fileSys;

        System.out.println(url);
    }

}
